package {
	import flash.display.BitmapData;
	import flash.geom.Point;
	import org.flixel.*;
	import flash.utils.ByteArray;
    import flash.net.FileReference;
	import org.flixel.plugin.photonstorm.FlxCollision;

	
	public class LevelEditor extends FlxState {
		// Test
		private var inkMap:BitmapData = new BitmapData(400, 300, true, 0);
		
		// Groups
		private var platforms:FlxGroup;
		private var platformsList:Vector.<Platform>;
		private var switches:FlxGroup;
		private var switchesList:Vector.<SwitchButton>;
		private var doors:FlxGroup;
		private var doorsList:Vector.<LockedDoor>;
		private var start:Door;
		private var end:Door;
		
		
		// Platform
		private var currPlatform:Platform;
		private var startPoint:Point;
		private var endPoint:Point;
		
		// For Switch/Door
		private var currSwitch:SwitchButton;
		private var currDoor:LockedDoor;
		private var currID:Number = 0;
		
		// Other objects here
		
		private var currObjId:uint;
		private var currObj:String;
		
		private var currTool:FlxText;
		
		
		override public function create():void {														
			FlxG.bgColor = 0xff888888;
			
			FlxG.mouse.show();
			
			currTool = new FlxText(5, 5, 80, "Current Tool: NONE", false);
			currTool.setFormat("Arial", 10, 0xffffff, null, 0);
			currObj = "";
			currTool.scrollFactor.x = currTool.scrollFactor.y = 0;
			platforms = new FlxGroup();
			platformsList = new Vector.<Platform>();
			switches = new FlxGroup();
			switchesList = new Vector.<SwitchButton>();
			doors = new FlxGroup();
			doorsList = new Vector.<LockedDoor>();
			
			add(platforms);
			add(currTool);
			add(switches);
			add(doors);
		}
		
		override public function update():void {
			// Keyboard Shortcuts
			
			// Make Starting
			if (FlxG.keys.ONE) {
				currObjId = 1;
			}
			// Make Ending
			if (FlxG.keys.TWO) {
				currObjId = 2;
			}
			// Make Platform
			if (FlxG.keys.THREE) {
				currObjId = 3;
			}
			// Make Switch/Door
			if (FlxG.keys.FOUR) {
				currObjId = 4;
			}
			
			// Set Display String
			switch(currObjId) {
				case 1:
					currObj = "Start Door";
					break;
				case 2:
					currObj = "End Door";
					break;
				case 3:
					currObj = "Platform";
					break;
				case 4:
					currObj = "Switch/Door";
					break;
			}
			// Set String
			currTool.text = "Current Tool: " + currObj;
			
			
			// Just Pressed
			if (FlxG.mouse.pressed()) {
				// Create/Move Start Door
				if (currObjId == 1) {
					if (!start) {
						start = new Door(FlxG.mouse.x, FlxG.mouse.y, false);
						add(start);
					}
					start.x = FlxG.mouse.x;
					start.y = FlxG.mouse.y;
				}
				if (currObjId == 2) {
					if (!end) {
						end = new Door(FlxG.mouse.x, FlxG.mouse.y, true);
						add(end);
					}
					end.x = FlxG.mouse.x;
					end.y = FlxG.mouse.y;
				}
			}
			
			// Creating platforms
			if (FlxG.mouse.justPressed() && currObjId == 3) {
				if (!startPoint) {
					startPoint = new Point(FlxG.mouse.x, FlxG.mouse.y);
				} else if (!endPoint) {
					endPoint = new Point(FlxG.mouse.x, FlxG.mouse.y);
					
					var origX:Number;
					var origY:Number;
					var width:Number;
					var height:Number;
					if (startPoint.x < endPoint.x)
						origX = startPoint.x;
					else
						origX = endPoint.x;
						
					if (startPoint.y < endPoint.y)
						origY = startPoint.y;
					else
						origY = endPoint.y;
					
					width = Math.abs(startPoint.x - endPoint.x);
					height = Math.abs(startPoint.y - endPoint.y);
					
					// Minimum width/height to avoid bitmap error
					if (width >= 2 && height >= 2) {
						currPlatform = new Platform(origX, origY, width, height, true);
						platforms.add(currPlatform);
						platformsList.push(currPlatform);
					}
					
					// Reset temporary markers
					startPoint = null;
					endPoint = null;
				}
			}
			
			// Creating switches and doors
			if (FlxG.mouse.justPressed() && currObjId == 4) {
				if (!currSwitch) {
					currID++;
					currSwitch = new SwitchButton(FlxG.mouse.x, FlxG.mouse.y, currID, false);
					switches.add(currSwitch);
					switchesList.push(currSwitch);
				} else if (!currDoor) {
					currDoor = new LockedDoor(FlxG.mouse.x, FlxG.mouse.y, currID, true);
					doors.add(currDoor);
					doorsList.push(currDoor);
					currSwitch = null;
					currDoor = null;
					
				}
				
			}
			
			// Save button "S" => create XML - String representation of the current level
			if (FlxG.keys.justPressed("S")) {
				var startX:Number
				var startY:Number
				var endX:Number
				var endY:Number
				if (start && end) {
					startX = start.x;
					startY = start.y;
					endX = end.x;
					endY = end.y;
				} else {
					// Missing Start and Finish, Don't let save!
					
				
				}
				var xmlString:String = "<?xml version = \"1.0\" encoding = \"utf-8\" ?>\n<level>";

				xmlString = xmlString + "\n\t<player x= '" + startX + "' y= '" + startY + "' c= '10'></player>";
				xmlString = xmlString + "\n\t<start x= '" + startX + "' y= '" + startY + "'></start>";
				xmlString = xmlString + "\n\t<finish x= '" + endX + "' y= '" + endY + "'></finish>";
				
				for each (var platform:Platform in platformsList) {
					xmlString = xmlString + "\n\t<platform x= '" + platform.x + "' y= '" + platform.y + "' w= '" + platform.width + "' h= '" + platform.height + "'></platform>";
				}
				for each (var button:SwitchButton in switchesList) {
					xmlString = xmlString + "\n\t<switchButton x= '" + button.x + "' y= '" + button.y + "' id= '" + button.ID + "' on= '" + button.isOn + "'></switchButton>";
				}
				for each (var door:LockedDoor in doorsList) {
					xmlString = xmlString + "\n\t<lockedDoor x= '" + door.x + "' y= '" + door.y + "' id= '" + door.ID + "' locked= '" + door.isLocked + "'></lockedDoor>";
				}
				
				saveLevel(xmlString + "\n</level>");
				
			}
			
			super.update();
		}	
		
		// Save current level as xml
		public function saveLevel(xmlString:String):void {
				var xml:XML;
				
				xml = new XML(xmlString);
				var ba:ByteArray = new ByteArray();
                ba.writeUTFBytes(xmlString);
				
				var fr:FileReference = new FileReference();

                fr.save(ba, "MyLevel.xml");
			
		}
		
	}
}